package com.tensquare.usr.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.usr.dao.UserDao;
import com.tensquare.usr.pojo.User;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.http.HttpRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private BCryptPasswordEncoder coders;
    @Autowired
    private HttpServletRequest request;
    public void sendsms(String mobile) {
        String code = RandomStringUtils.randomNumeric(6);
        //redis存一份
        redisTemplate.opsForValue().set("code_"+mobile,code,6, TimeUnit.HOURS);
        //用户存一份
        Map<String,String>map=new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code);
        rabbitTemplate.convertAndSend("sms",map);
    }


    /**
     * 查询全部列表
     * @return
     */
    public List<User> findAll() {
        return userDao.selectList(null);
    }


    /**
     * 条件查询+分页
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<User> findSearch(Map<String,Object> whereMap, int page, int size) {
        EntityWrapper<User> entityWrapper=new EntityWrapper();
        Set<String> set = whereMap.keySet();
        for (String s : set) {
            entityWrapper.eq(whereMap.get(s)!=null,s,whereMap.get(s));
        }
        Page<User>pageData=new Page<>(page,size);
        List<User> users = userDao.selectPage(pageData, entityWrapper);
        pageData.setRecords(users);
        return  pageData;
    }


    /**
     * 条件查询
     * @param whereMap
     * @return
     */
    public List<User> findSearch(Map<String,Object> whereMap) {
        return userDao.selectByMap(whereMap);
    }

    /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public User findById(String id) {
        return userDao.selectById(id);
    }

    /**
     * 增加
     * @param user
     */
    public void add(User user) {
        user.setId( idWorker.nextId()+"" );
        //密码加密
        user.setPassword(coders.encode(user.getPassword()));
        user.setFollowcount(0);//关注数
        user.setFanscount(0);//粉丝数
        user.setOnline(0L);//在线时长
        user.setRegdate(new Date());//注册日期
        user.setUpdatedate(new Date());//更新日期
        user.setLastdate(new Date());//最后登陆日期
        userDao.insert(user);
        redisTemplate.delete("code_" +user.getMobile());


    }

    /**
     * 修改
     * @param user
     */
    public void update(User user) {

        userDao.updateById(user);
    }

    public void deleteById(String id) {
        //无状态鉴权
        Claims claims_user = (Claims) request.getAttribute("claims_admin");
        if(claims_user==null){
            throw new RuntimeException("权限不足");
        }
        userDao.deleteById(id);
    }

    public User login(User user) {
        User userlogin=userDao.findByMobile(user.getMobile());
        if(userlogin!=null&&coders.matches(user.getPassword(),userlogin.getPassword())){
            return userlogin;
        }
        return null;
    }


    public void updateByUserAndFriend(String userid, String friendid, int x) {
        userDao.incFanscount(friendid,x);
        userDao.incFollowcount(userid,x);
    }
}
